package com.solcoo.customer.ui.shop;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.common.image.ImageLoader;
import com.android.common.util.DensityUtil;
import com.android.common.util.DialogUtil;
import com.android.common.util.StringUtil;
import com.solcoo.customer.ApplicationExt;
import com.solcoo.customer.Constant;
import com.solcoo.customer.R;
import com.solcoo.customer.base.BaseActivity;
import com.solcoo.customer.base.task.TaskHelper;
import com.solcoo.customer.base.task.TaskHelper.Callback;
import com.solcoo.customer.base.task.TaskHelper.DefaultCallback;
import com.solcoo.customer.model.ShopInfo;
import com.solcoo.customer.ui.level.LevelActivity;
import com.solcoo.customer.ui.order.OrderActivity;
import com.solcoo.customer.util.CheckUtil;
import com.solcoo.customer.util.CheckUtil.CheckResult;

public class ShopDetailActivity extends BaseActivity {

	private String shopNo;

	ImageView icon;
	TextView name;
	TextView level;
	TextView discount;
	TextView balance;
	TextView address;
	TextView tel;

	private Dialog callDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shop_detail);
		shopNo = getIntent().getStringExtra(Constant.IntentKey.SHOP_NO);
		if (StringUtil.isBlank(shopNo)) {
			return;
		}
		TaskHelper.loadView(this, Constant.URL.getShopDetail(
				ApplicationExt.getUser().yonghum, shopNo), callback,
				ShopInfo.class);
		icon = (ImageView) findViewById(R.id.shop_icon);
		name = (TextView) findViewById(R.id.shop_name);
		level = (TextView) findViewById(R.id.level);
		discount = (TextView) findViewById(R.id.discount);
		balance = (TextView) findViewById(R.id.balance);
		address = (TextView) findViewById(R.id.address);
		tel = (TextView) findViewById(R.id.tel);
		callDialog = DialogUtil.createDialog(this, callOnClickListener,
				DialogUtil.onNegativeListener, null, "立即拨打", null);
	}

	private Callback<ShopInfo> callback = new DefaultCallback<ShopInfo>(this) {

		@Override
		public void onSuccess(ShopInfo result) {
			ImageLoader.getInstance().load(result.yonghu.touxiang, icon);
			name.setText(result.yonghu.dianming);
			address.setText(result.yonghu.dizhi);
			tel.setText(result.yonghu.lianxifs);
			level.setText(String.valueOf(result.zhanghu.dengji));
			discount.setText(String.valueOf(result.zhanghu.zhekoul));
			balance.setText(String.valueOf(result.zhanghu.yue));
			fillExp(result);
		}

		private void fillExp(ShopInfo result) {
			TextView expValue = (TextView) findViewById(R.id.exp_value);
			TextView maxExp = (TextView) findViewById(R.id.max_exp);
			View expBg = findViewById(R.id.experience_bg);
			View expImage = findViewById(R.id.experience);

			expValue.setVisibility(View.VISIBLE);
			maxExp.setVisibility(View.VISIBLE);
			expBg.setVisibility(View.VISIBLE);
			expImage.setVisibility(View.VISIBLE);

			expValue.setText(String.valueOf(result.zhanghu.jingyanz));
			int currentExp = DensityUtil.dip2px(30);
			if (result.jyz == -1) {
				maxExp.setText("∞");
			} else {
				maxExp.setText(String.valueOf(result.jyz));
				currentExp = Math
						.max(currentExp,
								(int) (result.zhanghu.jingyanz
										* expBg.getWidth() / result.jyz));
				currentExp = Math.min(expBg.getWidth(), currentExp);
			}
			expImage.getLayoutParams().width = currentExp;
		}

	};

	public void levelOnClick(View v) {
		Intent it = new Intent(v.getContext(), LevelActivity.class);
		it.putExtra(Constant.IntentKey.SHOP_NO, shopNo);
		startActivity(it);
	}

	public void accountOnClick(View v) {
		Intent it = new Intent(v.getContext(), OrderActivity.class);
		it.putExtra(Constant.IntentKey.SHOP_NO, shopNo);
		startActivity(it);
	}

	public void telOnClick(View v) {
		String number = tel.getText().toString();
		CheckResult mCheckResult = CheckUtil.checkTel(number);
		if (mCheckResult.isSuccess()) {
			callDialog.show();
		}
	}

	public static final OnItemClickListener shopOnItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View v, int position,
				long id) {
			String shopNo = v.getContentDescription().toString();
			Intent it = new Intent(v.getContext(), ShopDetailActivity.class);
			it.putExtra(Constant.IntentKey.SHOP_NO, shopNo);
			v.getContext().startActivity(it);
		}
	};

	private DialogInterface.OnClickListener callOnClickListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			String number = tel.getText().toString();
			Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
					+ number));
			startActivity(intent);
		}
	};
}
